package com.sbtr.business.classify.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sbtr.business.classify.entity.AlgoTemplateFile;
import com.sbtr.business.classify.entity.AlgoVersionFile;
import com.sbtr.business.classify.dto.AlgoVersionFileDto;

import com.sbtr.business.classify.mapper.AlgoTemplateFileMapper;
import com.sbtr.business.classify.mapper.AlgoTemplateMapper;
import com.sbtr.business.classify.service.AlgoTemplateFileService;
import com.sbtr.business.classify.service.AlgoVersionFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbtr.business.classify.mapper.AlgoVersionFileMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sbtr.common.PageDto;
import com.sbtr.util.PageDtoUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 算法版本字段列表 Service接口实现类
 *
 * @author dgl
 * @since  2023-06-13
 */
@Transactional
@Service
public class AlgoVersionFileServiceImpl extends ServiceImpl<AlgoVersionFileMapper,AlgoVersionFile> implements AlgoVersionFileService {

    @Resource
    AlgoVersionFileMapper algoVersionFileMapper;
    @Resource
    AlgoTemplateFileMapper algoTemplateFileMapper;
    @Override
    public PageDto<AlgoVersionFile> pageList(AlgoVersionFileDto algoVersionFileDto) {

        IPage<AlgoVersionFile> page = new Page<>();
        page.setSize(algoVersionFileDto.getPageSize());
        page.setCurrent(algoVersionFileDto.getPage());
        page = algoVersionFileMapper.selectPages(page, BeanUtil.beanToMap(algoVersionFileDto));
        PageDto<AlgoVersionFile> r = PageDtoUtils.getPageDtoByIPage(page, AlgoVersionFile.class);

        return r;
    }

    @Override
    public AlgoVersionFile download(String id) {
        AlgoVersionFile algoVersionFile1 = this.getById(id);
        algoVersionFile1.setCounts(algoVersionFile1.getCounts() + 1);
        this.updateById(algoVersionFile1);

        AlgoTemplateFile algoTemplateFile = algoTemplateFileMapper.selectById(algoVersionFile1.getTemplateId());
        algoTemplateFile.setCounts(algoTemplateFile.getCounts() + 1);
        algoTemplateFileMapper.updateById(algoTemplateFile);
        return  algoVersionFile1;
    }
}

